

package com.anzym.android.kegdroidkey;

import com.anzym.android.kegdroidlibrary.database.BeerDataSource;
import com.anzym.android.kegdroidlibrary.models.BeerOrder;
import com.anzym.android.kegdroidlibrary.models.KegDroidUser;
import com.anzym.android.kegdroidlibrary.tasks.FetchBeerTask;
import com.anzym.android.kegdroidlibrary.R;
import android.app.Application;
import android.content.SharedPreferences;
import android.util.Log;

public class KegDroidKeyApplication extends Application {

    private static String TAG = "KegDroidApplication";
    private static final String KEGDROID_PREFS = "KegDroidPrefs";
    private static KegDroidKeyApplication instance;
    private SharedPreferences settings;
    
    public static int LARGE_GLASS = 12;
    public static int MEDIUM_GLASS = 8;
    public static int SMALL_GLASS = 2;

    public KegDroidKeyApplication () {
        this.beerOrder = new BeerOrder();
        this.kdUser = new KegDroidUser();
    }
    public static KegDroidKeyApplication getInstance() {
        return instance;
    }
    
    KegDroidUser kdUser;
    BeerOrder beerOrder;
    
    
    
    public KegDroidUser getUser() {
        return this.kdUser;
    }
    
    public BeerOrder getBeerOrder() {
        return this.beerOrder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        KegDroidKeyApplication.instance = this;
        kdUser = new KegDroidUser();
        Log.d(TAG, "Setting data from Prefs");
        settings = getSharedPreferences(KEGDROID_PREFS, 0);
        kdUser.setEmailAddress(settings.getString("email_address", getResources()
                .getString(R.string.default_email)));
        kdUser.setGPlusId(settings.getString("gplus_id", getResources()
                .getString(R.string.default_id)));
        kdUser.setImageUrl(settings.getString("image_url", null));
        kdUser.setGivenName("");
        Log.d(TAG, "onCreate() email: " + kdUser.getEmailAddress() + "  gplusId: "
                + kdUser.getGPlusId());
        
        BeerDataSource beerDBS = new BeerDataSource(this);
        beerDBS.open();
        if (beerDBS.checkDBEmpty()) {
            FetchBeerTask fetchBeerTask = new FetchBeerTask(getApplicationContext());
            fetchBeerTask.execute();
        }
        beerDBS.close();
    }

    public SharedPreferences getSettings() {
        return settings;
    }

    public void setSettings(SharedPreferences settings) {
        this.settings = settings;
    }

    public static String getKegdroidPrefs() {
        return KEGDROID_PREFS;
    }
    
    

}
